﻿#region License
// (c) Garry McGlennon 2009
// 
// This project is made available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// 
// View the project and the latest code at http://dnpframework.codeplex.com/
// 
// All other rights reserved.
// 
// blog: www.dotNetProfessional.com/blog/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using dnp.Framework.Web;
using System.Linq;
using MbUnit.Framework;

namespace dnp.Framework.UnitTest.Web
{
    [TestFixture]
    public class WebLogFileTest
    {
        [Test]
        [Ignore("This is a very long running test - only activate if modifying the WebLogFile class")]
        public void IISLog()
        {
            string path = Environment.CurrentDirectory;

            WebLogFile parser = new WebLogFile(path + @"\Web\LogData\IISLogFile.txt", new IISWebLogFileProvider());


            //var result = (from entry in parser
            //              where entry.UserAgent.Contains("google")
            //              select new { entry.UriStem }).Distinct();
            //             //select new { entry.UriStem };

            var spiders = new List<string> { "google", "msn", "yahoo" };

            Stopwatch sw = new Stopwatch();
            sw.Start();
            var result = (from entry in parser
                          where entry.UserAgent.Contains(spiders)
                          select new { entry.UserAgent }).Distinct()
                          ;

            var list = result.ToList();
            sw.Stop();

            long ticks = sw.ElapsedTicks;

            Debug.WriteLine("Count: " + list.Count);
            Debug.WriteLine("Ticks:" + ticks + ", Milliseconds:" + sw.ElapsedMilliseconds);

        }

    }

    public static class Extensions
    {
        public static bool Contains(this string source, IEnumerable<string> list)
        {
            foreach (string item in list)
                if (source.Contains(item))
                    return true;

            return false;
        }
    }
}
